/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
  <file> UlsStream.h </file>
  <brief>
 	The wrapper class of ulslex_t
    This file is part of ULS, Unified Lexical Scheme.
  </brief>
  <author>
    Stanley J. Hong <babel96@gmail.com>, 2011.
  </author>
*/

#include "UlsLex.h"
#include <iostream>
#include <stdarg.h>

#ifndef __ULSCPP_STREAM_H__
#define __ULSCPP_STREAM_H__

namespace ULS {
	class UlsStream {
		uls_lex_t    *uls;
		uls_stream_t uls_hdr;
		int fd_out;

		// <brief>
		// reads the head informaton of uls-file indicated by 'fd' 
		// stores it to the field 'uls_hdr'.
		// </brief>
		// <parm name="fd">the file-descriptor of uls-stream file (*.uls)</parm>
		// <return>none</return>
		void readUlsHeader(int fd);

	public:
		// <brief>
		// A constructor which accepts UlsLex as first parameter.
		// 'UlsLex' is the the class for lexical analysis defined in the same namespace 'ULS'.
		// </brief>
		// <parm name="ulslex">An object for lexical analysis</parm>
		// <return>none</return>
		UlsStream(UlsLex& ulslex);

		// <brief>
		// The destuctor of UlsStream.
		// </brief>
		// <return>none</return>
		virtual ~UlsStream();

		// <brief>
		// This create a uls-file, output file, named by 'filepath' and
		// writes some attributes to the ULS-header in the file.
		// </brief>
		// <parm name="filepath">The name of output-file.</parm>
		// <parm name="mode">The type of file, The default value ULS_STREAM_BIN_LE is for creating the file of little-endian</parm>
		// <parm name="subtag">The value of this is stored in an attribute in uls-file as 'TAG:'</parm>
		// <return>true/false</return>
		bool createStream(std::string& filepath, int mode=ULS_FL_LINE_NUMBERING|ULS_STREAM_BIN_LE, std::string subtag="");

		// <brief>
		// starts streaming and store the sequence of tokens to filepath.
		// The sequence constitutes the body of the file.
		// You can call this method multiple times with different 'filepath'.
		// </brief>
		// <parm name="filepath">The path of input file</parm>
		// <return>0 if it succeeds, otherwise -1</return>
		int startStream(std::string& filepath);
	
		// <brief>
		// This finalizes the streaming and closes the output-file.
		// </brief>
		// <return>none</return>
		void closeStream(void);
	};
}

#endif // __ULSCPP_STREAM_H__
